# encoding: utf-8
#@author: sydr
#@file: CPA_evaluation.py
#@time: 2025/3/27 15:04
import json
import random

import math
import mitmproxy

from mitmproxy.http import Request, Response
from mitmproxy import ctx


class CPAEvaluation:
    def request(self, flow: mitmproxy.http.HTTPFlow):
        r:Request = flow.request


    def response(self, flow: mitmproxy.http.HTTPFlow):
        res: Response = flow.response
        r: Request = flow.request
        if "/courseapi/chapter/front/selfCourseTree" in r.pretty_url:
            data = json.loads(res.get_text())
            process=0.5

            totalChapters=0
            for p in data["data"]["chapterTree"]:
                if len(p["children"])>0:
                    totalChapters+=len(p["children"])

            totalChaptersList=[ i for i in range(totalChapters)]
            newList=[]
            process=math.ceil(totalChapters*process)
            print("总课卡数："+str(totalChapters))
            print("进度卡数："+str(process))
            for i in range(process):
                random.shuffle(totalChaptersList)
                newList.append(totalChaptersList[0])
                totalChaptersList.pop(0)
            j=0
            for p in data["data"]["chapterTree"]:
                if len(p["children"])>0:
                    for ch in p["children"]:
                        if j  in newList:
                            ch["isFinished"]=1
                        else:
                            ch["isFinished"]=0

                        j+=1
            res.text = json.dumps(data)
        #
        # if "courseapi/evaluation/front/courseEvaluationTemplate" in r.pretty_url:
        #     data = json.loads(res.get_text())
        #     data["data"]["hasEvaluatedRulesTypeStr"]=""
        #     data["data"]["hasPopupsRulesTypeStr"]=""
        #     res.text = json.dumps(data)


addons = [

# CPAEvaluation(),

]
